import React from 'react';
import {toJS} from 'mobx';
import {inject, observer} from 'mobx-react';
import {Button} from 'antd';
import {WeaTop, WeaRightMenu, WeaTab, WeaSearchGroup, WeaInput, WeaSelect} from 'ecCom';
import {WeaSwitch} from 'comsMobx'
import {Form} from 'antd';
import {WeaTableNew} from 'comsMobx';

const WeaTable = WeaTableNew.WeaTable;
import {WeaLocaleProvider} from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
import * as workplanUtil from '../../util/workplanUtil';
import AddWorkPlanShare from './AddWorkPlanShare';

@inject('workPlanSharePersonalStore')
// @inject('comsWeaTableStore')
@observer
class WorkPlanSharePersonal extends React.Component {

  //在第一次渲染时加载接收人共享的相关数据和初始化添加相应的添加共享表单域
  componentDidMount() {
    const {location, workPlanSharePersonalStore} = this.props;

    workPlanSharePersonalStore.initData({
      CompParams: {viewcondition: 0}
    });
    workPlanSharePersonalStore.setAddWorkPlanShareConditionAndInterfaceOfAddWPShare(0);
  }

  componentWillReceiveProps(nextProps) {
    if(nextProps.location && this.props.location.key !== nextProps.location.key) {
        const { workPlanSharePersonalStore } = this.props;
        workPlanSharePersonalStore.doSearch()
    }
  }

  componentWillUnmount() {
    const { workPlanSharePersonalStore } = this.props;
    workPlanSharePersonalStore.setShowSearchAd(false);
  }

  onEnterSearch = () => {
    const {workPlanSharePersonalStore} = this.props;

    workPlanSharePersonalStore.doSearch();
    workPlanSharePersonalStore.setShowSearchAd(false);
  }

  render() {
    let topTab = [
      {
        groupid: 'flowAll',
        title: `${getLabel(384927, '接收人共享')}`,
        viewcondition: 0
      },
      {
        groupid: 'flowNew',
        title: `${getLabel(384928, '创建人共享')}`,
        viewcondition: 1
      },
    ];
    const {workPlanSharePersonalStore} = this.props;
    const {comsWeaTableStore} = workPlanSharePersonalStore;
    let collectParams={
      favname: getLabel(20190, '日程共享'),
      favouritetype: 5,
      objid: 0,
      importantlevel: 1,
    }
    return (
      <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@rqjxmx`} datas={workplanUtil.getRightMenu(workPlanSharePersonalStore, comsWeaTableStore)}  collectParams={collectParams}>
        <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@mhwred`}
          title={getLabel(20190, '日程共享')}
          loading={workPlanSharePersonalStore.loading}
          icon={<i className='icon-coms-schedule'/>}
          iconBgcolor='#7734FF'
          buttons={workplanUtil.getTopButtons(workPlanSharePersonalStore, getLabel(611, '添加'), comsWeaTableStore)}
          showDropIcon={true}
          dropMenuDatas={workplanUtil.getRightMenu(workPlanSharePersonalStore, comsWeaTableStore)}
        />
          <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@5m6ynd`}
            datas={topTab}
            keyParam="viewcondition"  //主键
            countParam="groupid" //数量
            searchType={['advanced']}
            selectedKey={workPlanSharePersonalStore.viewcondition}
            showSearchAd={workPlanSharePersonalStore.ifShowSearchAd}
            //这里接收的是切换显隐之后相应的bool值
            setShowSearchAd={bool => {
              workPlanSharePersonalStore.setShowSearchAd(bool)
            }}
            buttonsAd={workplanUtil.getTabButtonsAd(workPlanSharePersonalStore)}
            searchsAd={workplanUtil.getSearchs(workPlanSharePersonalStore, this.onEnterSearch)}
            // searchsAd={this.getSearchs()}
            onChange={(key) => {
              workPlanSharePersonalStore.initData({
                CompParams: {
                  viewcondition: key
                }
              });
              //这个用来改变添加共享的表单域
              workPlanSharePersonalStore.setAddWorkPlanShareConditionAndInterfaceOfAddWPShare(key);
            }}
            advanceHeight={workPlanSharePersonalStore.viewcondition == '1' ? 180 : 120}
            dropMenuProps={{ collectParams:collectParams}}
          />
          <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@76ewft`}
            sessionkey={workPlanSharePersonalStore.dataKey}
            hasOrder={true}
            needScroll={true}
            onOperatesClick={this.onOperatesClick}
            comsWeaTableStore={comsWeaTableStore}
          />
        <AddWorkPlanShare ecId={`${this && this.props && this.props.ecId || ''}_AddWorkPlanShare@hjmwyn`}/>
      </WeaRightMenu>
    )
  }

  onOperatesClick = (record, index, operate, flag, argumentString) => {
    let workPlanSharePersonalStore = this.props.workPlanSharePersonalStore;
    let fn;
    if (operate.index === '0') {//删除
      fn = workplanUtil.doMultiDelete;
    }
    fn(...['MyDoc', record.randomFieldId, workPlanSharePersonalStore]);
  }

}

export default WorkPlanSharePersonal